package com.eastfair.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.home.service.CategoryService;
import com.eastfair.venuebooking.api.ServiceCategoryFeign;
import com.eastfair.venueservice.dto.ServiceCategoryDTO;
import com.eastfair.venueservice.dto.ServiceCategoryPageQuery;
import com.eastfair.venueservice.entity.WorkOrderServiceItem;
import com.eastfair.venueservice.vo.ServiceCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author lyw
 * @Date 2022/8/8 11:59
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private ServiceCategoryFeign serviceCategoryFeign;

    /**
     * 查询服务分类列表分页
     * @param params
     * @return
     */
    @Override
    public Page<ServiceCategoryVO> queryCategoryList(PageParams<ServiceCategoryPageQuery> params) {
        log.info("CategoryServiceImpl +++++ queryServiceCategoryList - 查询服务分类列表分页 params={}", JSONObject.toJSON(params));
        R<Page<ServiceCategoryVO>> r = serviceCategoryFeign.queryServiceCategoryList(params);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 保存服务分类
     * @param serviceCategoryDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveCategoryInfo(ServiceCategoryDTO serviceCategoryDTO) {
        log.info("CategoryServiceImpl +++++ saveCategoryInfo - 保存服务分类 params={}", JSONObject.toJSON(serviceCategoryDTO));
        serviceCategoryDTO.setCategoryType(BusinessConstant.YES);
        R<Boolean> r = serviceCategoryFeign.saveServiceCategoryInfo(serviceCategoryDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 修改服务分类
     * @param serviceCategoryDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCategoryById(ServiceCategoryDTO serviceCategoryDTO) {
        log.info("CategoryServiceImpl +++++ updateCategoryById - 修改服务分类 params={}", JSONObject.toJSON(serviceCategoryDTO));
        R<Boolean> r = serviceCategoryFeign.updateServiceCategoryById(serviceCategoryDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 删除服务分类
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delCategoryById(Long id) {
        log.info("CategoryServiceImpl +++++ delCategoryById - 删除服务分类 params={}", id);
        R<Boolean> r = serviceCategoryFeign.delServiceCategoryById(id);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 服务分类列表
     * @param categoryType
     * @return
     */
    @Override
    public List<ServiceCategoryVO> getCategoryListByExhibitionManageId(Integer categoryType,Long exhibitionManageId) {
        log.info("CategoryServiceImpl +++++ getCategoryList - 删除服务分类 params={}", categoryType);
        R<List<ServiceCategoryVO>> r = serviceCategoryFeign.getCategoryListByExhibitionManageId(categoryType,exhibitionManageId);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return  r.getData();
    }
}
